-- 获取滑动窗口大小和限流大小
local min = ARGV[1]
local max = ARGV[2]
local maxCount = ARGV[3]
local accessKey = ARGV[4]
local member = ARGV[5]
--生成滑动窗口key
local windowKey = 'gateway:slidingWindow:user:' .. accessKey
-- 如果滑动窗口不存在就创建
redis.call('zadd', windowKey, 'nx', max, member)
-- 查询滑动窗口请求数量
local count = redis.call('zcount', windowKey, min, max)
redis.call('ZREMRANGEBYSCORE', windowKey, 0, min)
if(count >= tonumber(maxCount)) then
    return 0
else
    redis.call('zadd', windowKey, max, member)
    return 1
end